﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using TakeoutMangesBLL;
using Model;

namespace TakeoutMangesWeb
{
    public partial class Shoptype : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Usersession"] != null)
            {
                logininfo.Attributes.Add("Style", "display:none");
                showinfo.Attributes.Add("Style", "display:block");
                Models.Usersession us = Session["Usersession"] as Models.Usersession;
                user.InnerHtml = us.UserName;
            }
            else
            {
                logininfo.Attributes.Add("Style", "display:block");
                showinfo.Attributes.Add("Style", "display:none");
            }

            if (!IsPostBack)
            {
                MaxPageNumber();
                shoptype();
            }
        }

        //最大页码
        public int MaxPageNumber()
        {
            int PageCount = 1;
            int id = Convert.ToInt32(Request.QueryString["id"]);
            List<Model.Product> list=ProductBLL.Getcount(id);
            double de = list.Count/ (PageCount * 1.0);
            int aa = Convert.ToInt32(Math.Ceiling(de));//取上限
            return aa;
        }

        public void shoptype()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1, 1);
            Repeaters.DataBind();
            Label1.Text ="1";
            Label2.Text= MaxPageNumber().ToString();
            for (int i = 1; i <=MaxPageNumber(); i++)
            {
                DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString()));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["Usersession"] = null;
            Session.Remove("Usersession");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("ShopCar.aspx");
        }
        //快速跳转
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1,Convert.ToInt32(DropDownList1.SelectedValue));
            Repeaters.DataBind();
            Label1.Text =DropDownList1.SelectedValue;
        }

        //跳转到最后一页
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1, MaxPageNumber());
                Repeaters.DataBind();
                Label1.Text = MaxPageNumber().ToString();
                DropDownList1.SelectedValue= MaxPageNumber().ToString();
        }

        //跳转到第一页
        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1, 1);
            Repeaters.DataBind();
            Label1.Text = "1";
            DropDownList1.SelectedValue = "1";
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            int pagec = Convert.ToInt32(Label1.Text) - 1;
            if (pagec <= 0)//判断是不是第一页，是的话什么也不干
            {
                return;
            }
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1, pagec);
            Repeaters.DataBind();
            Label1.Text = pagec.ToString();
            DropDownList1.SelectedValue= pagec.ToString();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            int pagec = Convert.ToInt32(Label1.Text) + 1;
            if (pagec > MaxPageNumber())//判断是不是第一页，是的话什么也不干
            {
                return;
            }
            int id = Convert.ToInt32(Request.QueryString["id"]);
            Repeaters.DataSource = ProductBLL.GetProductCategory(id, 1, pagec);
            Repeaters.DataBind();
            Label1.Text = pagec.ToString();
            DropDownList1.SelectedValue = pagec.ToString();
        }
    }
}